import { Component, OnInit, EventEmitter, Output, Input, OnDestroy } from '@angular/core';
import { MapDataService, MARKER_TYPE, OVERLAY_KEY } from '../service/map-data.service';
import { NotificationService } from 'app/shared/service';

@Component({
    selector: 'itmp-alert-overlay',
    templateUrl: './alert-overlay.component.html',
    styleUrls: ['./alert-overlay.component.css']
})
export class AlertOverlayComponent implements OnInit, OnDestroy {
    @Input() currentAlert = null;
    @Input() mainMap = null;
    @Output() alertInteraction = new EventEmitter<boolean>();

    clickLoading = false;
    overlayKey = OVERLAY_KEY.ALERT_OVERLAY;

    constructor(private mapDataService: MapDataService, private msg: NotificationService) {}

    alertOperation(flag: boolean) {
        const zoom = this.mainMap.getView().getZoom();
        if (zoom < 19) {
            this.msg.notify({
                type: 'error',
                path: 'Gis.Gis.Zoom_Notify'
            });
            return;
        }

        this.clickLoading = true;
        const params = {
            alertId: this.currentAlert.alertID,
            ackValue: flag,
            userName: localStorage.getItem('user_name')
        };
        this.mapDataService.alertAck(params).subscribe(
            res => {
                this.clickLoading = false;
                this.alertInteraction.emit(flag);
            },
            err => {
                this.clickLoading = false;
            }
        );

        setTimeout(() => (this.clickLoading = false), 10000);
    }

    ngOnDestroy() {}
    ngOnInit() {}
}
